package com.yangjun.service.departmentCustomization;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.DVConstants;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
import com.yangjun.utils.DvUtils;
/**
 * 
 * @ClassName PreSalesPharmacistService
 * @Description 可视化部门定制售前科组单品转化的内容
 * @author Administrator
 * @Date 2017年3月15日 上午10:35:01
 * @version 1.0.0
 */
@Service
public class PreSalesRecommendProductService extends DepartmentCustomizationCommonService{
	
	public Map<String,Object> queryRank(String groupName){
		Map<String,Object> resultMap = Maps.newHashMap();
		List<PreSalesSectionOverViewEntity> recommendKeywordRankList = this.preSalesSectionMapper.queryRecommendKeywordRank(groupName);
		List<PreSalesSectionOverViewEntity> recommendStaffRankList = this.preSalesSectionMapper.queryRecommendStaffRank(groupName);
		resultMap.put("keywordRank",rankPreSalesSectionOverViewEntityList(recommendKeywordRankList));
		resultMap.put("staffRank",rankPreSalesSectionOverViewEntityList(recommendStaffRankList));
		return resultMap;
	}
	public List<Object[]> queryRecommendKeywordTable(String groupName,String adviceKeyword){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> recommendKeywordRankList = this.preSalesSectionMapper.queryRecommendKeywordTable(groupName,adviceKeyword);
		if(recommendKeywordRankList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : recommendKeywordRankList){
				String staffName = preSalesSectionOverViewEntity.getStaffName();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt()==null?0:preSalesSectionOverViewEntity.getAdviceCnt();
				int recommendCnt= preSalesSectionOverViewEntity.getRecommendCnt()==null?0:preSalesSectionOverViewEntity.getRecommendCnt();
				float recommendRate = preSalesSectionOverViewEntity.getRecommendRate()==null?0f:preSalesSectionOverViewEntity.getRecommendRate();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt()==null?0:preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount()==null?0f:preSalesSectionOverViewEntity.getSalesOrderAmount();
				Object[] dataArr = new Object[]{staffName,adviceCnt,recommendCnt,recommendRate,orderCnt,salesOrderAmount};
				if(arrIsExistNull(2,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		DvUtils.PinYinComparatorSortList(resultList);
		return resultList;
	}
	public List<Object[]> queryRecommendStaffTable(String groupName,String staffName){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> recommendStaffRankList = this.preSalesSectionMapper.queryRecommendStaffTable(groupName,staffName);
		if(recommendStaffRankList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : recommendStaffRankList){
				String adviceKeyword = preSalesSectionOverViewEntity.getAdviceKeyword();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt()==null?0:preSalesSectionOverViewEntity.getAdviceCnt();
				int recommendCnt= preSalesSectionOverViewEntity.getRecommendCnt()==null?0:preSalesSectionOverViewEntity.getRecommendCnt();
				float recommendRate = preSalesSectionOverViewEntity.getRecommendRate()==null?0f:preSalesSectionOverViewEntity.getRecommendRate();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt()==null?0:preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount()==null?0f:preSalesSectionOverViewEntity.getSalesOrderAmount();
				Object[] dataArr = new Object[]{adviceKeyword,adviceCnt,recommendCnt,recommendRate,orderCnt,salesOrderAmount};
				if(arrIsExistNull(2,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		DvUtils.PinYinComparatorSortList(resultList);
		return resultList;
	}

	public Map<String,Map<String,List<String[]>>> queryAdviceKeywordTableData(String groupName,String staffName){
		Map<String,Map<String,List<String[]>>> resultMap = Maps.newHashMap();
		resultMap.put("month", queryRecommendTableDataByMonth(groupName,null,staffName,"adviceKeyword"));
		resultMap.put("year", querySectionTableDataByYear(groupName,null,staffName,"adviceKeyword"));

		return resultMap;
	}
	public Map<String,Map<String,List<String[]>>> queryStaffTableData(String groupName,String adviceKeyword){
		Map<String,Map<String,List<String[]>>> resultMap = Maps.newHashMap();
		resultMap.put("month", queryRecommendTableDataByMonth(groupName,adviceKeyword,null,"staffName"));
		resultMap.put("year", querySectionTableDataByYear(groupName,adviceKeyword,null,"staffName"));
		return resultMap;
	}
	/**
	 * 
	 * @Description （近12个月）
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:38:13
	 */
	public Map<String,List<String[]>> queryRecommendTableDataByMonth(String groupName,String adviceKeyword,String staffName,String deminsion){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryRecommendProductTableData(groupName,adviceKeyword,staffName,beginDate,endDate,"MONTH");
		return queryTableData(tableByDayList,DVConstants.MONTH_DAY,deminsion,"month");
	}
	/**
	 * @Description （近1年）
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:38:31
	 */
	public Map<String,List<String[]>> querySectionTableDataByYear(String groupName,String adviceKeyword,String staffName,String deminsion){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_YEAR,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryRecommendProductTableData(groupName,adviceKeyword,staffName,beginDate,endDate,"YEAR");
		return queryTableData(tableByDayList,DVConstants.MONTH,deminsion,"year");
	}
}
